package config

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"sync"
)

type Script struct {
	Exe  string `yaml:"exe"`
	Dir  string `yaml:"dir"`
	Name string `yaml:"name"`
}

type ScriptConfig struct {
	Scripts map[string]Script `yaml:"scripts"`
	mutex   *sync.Mutex
}

func (s *ScriptConfig) Get(key string) *Script {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	if value, ok := s.Scripts[key]; ok {
		return &value
	}
	return nil

}

var S *ScriptConfig

func init() {
	S = &ScriptConfig{}

	if data, err := ioutil.ReadFile("script.yaml"); err == nil {

		_ = yaml.Unmarshal([]byte(data), S)
	}

	S.mutex = &sync.Mutex{}

	log.Println("加载脚本", S)

}

type LockMapData struct {
	C            chan int
	Status       int    // 是否执行成功, 1为成功，2为不成功
	ResponseInfo string // 如果出错则 错误信息存储到此字段
}

type LockStruct struct {
	Mux     *sync.Mutex
	Count   int // 计数器
	LockMap map[int]*LockMapData
}

var Lock *LockStruct
